package com.itheima.common.utils;

import io.jsonwebtoken.*;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/***
 * @Author： shenkunlin
 * @Description: itheima-leadnews com.itheima.common.util
 *      1)JWT令牌创建
 *      2)JWT令牌识别
 *      3)JWT令牌校验
 * @Date 2021/6/23 : 14:06
 ****/
public class JwtUtils {

    //秘钥
    private static final String key ="MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY";


    /***
     * 令牌解析
     * @return
     */
    public static Map<String, Object> parse(String token){
        //创建令牌解析对象
        JwtParser parser = Jwts.parser();
        //设置秘钥信息
        parser.setSigningKey(getKey());
        //解析
        Jws<Claims> claimsJws = parser.parseClaimsJws(token);
        return claimsJws.getBody();
    }

    /***
     * 令牌校验
     * @return
     */
    public static Boolean checkToken(String token){
        try {
            //创建令牌解析对象
            JwtParser parser = Jwts.parser();
            //设置秘钥信息
            parser.setSigningKey(getKey());
            //解析
            Jws<Claims> claimsJws = parser.parseClaimsJws(token);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /***
     * 1)JWT令牌创建
     *      1:载荷信息  有效期|唯一识别号|主题|颁发者等等
     *      2:签名算法
     *      3:创建令牌
     */
    public static String token(Map<String,Object> dataMap){
        JwtBuilder builder = Jwts.builder();
        //载荷信息【自带载荷】
        builder.setId(UUID.randomUUID().toString()); //唯一标识
        builder.setExpiration(new Date(System.currentTimeMillis()+36000000)); //有效期
        builder.setSubject("用户身份鉴定令牌");//主题
        if(dataMap!=null){
            builder.addClaims(dataMap);//创建载体
        }
        //签名->算法->秘钥
        builder.signWith(SignatureAlgorithm.HS512,getKey());
        //创建令牌
        return builder.compact();
    }


    /***
     * 1)JWT令牌创建
     *      1:载荷信息  有效期|唯一识别号|主题|颁发者等等
     *      2:签名算法
     *      3:创建令牌
     */
    public static String token(){
        return token(null);
    }

    /***
     * 获取秘钥信息
     */
    private static SecretKey getKey(){
        /***
         * 创建SecretKeySpec
         * 1：秘钥的字节数组
         * 2：从秘钥哪里开始读取
         * 3：读到秘钥哪里结束
         * 4：加密算法 ->AES
         */
        SecretKey secretKey = new SecretKeySpec(key.getBytes(), 0,key.getBytes().length, "AES");
        return secretKey;
    }

    public static void main(String[] args) {
        //创建令牌
//        String token = token();
//        System.out.println(token);

        //解析令牌
        String token = "eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiJjMTg1YzEzNi05YzY4LTQ1MTktYTZiMy0wNzc5Nzg4OWM2YzIiLCJleHAiOjE2Mjk2NTMyMDksInN1YiI6IueUqOaIt-i6q-S7vemJtOWumuS7pOeJjCIsImltYWdlIjoiMSIsInNhbHQiOiIzMXN5SnhZODRWIiwiYXBVc2VySWQiOjMsInR5cGUiOjAsInNjb3JlIjpudWxsLCJwYXNzd29yZCI6IioiLCJsb2dpblRpbWUiOm51bGwsInBob25lIjoiMTM1MTEyMjM0NTUiLCJuYW1lIjoiemhhbmdzYW4iLCJuaWNrbmFtZSI6bnVsbCwiY3JlYXRlZFRpbWUiOnsibW9udGgiOiJBVUdVU1QiLCJ5ZWFyIjoyMDIxLCJkYXlPZk1vbnRoIjoyMCwiaG91ciI6MjAsIm1pbnV0ZSI6NDEsIm1vbnRoVmFsdWUiOjgsIm5hbm8iOjAsInNlY29uZCI6MTAsImRheU9mV2VlayI6IkZSSURBWSIsImRheU9mWWVhciI6MjMyLCJjaHJvbm9sb2d5Ijp7ImlkIjoiSVNPIiwiY2FsZW5kYXJUeXBlIjoiaXNvODYwMSJ9fSwibG9jYXRpb24iOm51bGwsImlkIjo2LCJhcEF1dGhvcklkIjpudWxsLCJlbWFpbCI6bnVsbCwic3RhdHVzIjpudWxsfQ.EUFHzzcsbamsEWmUvCaIyQ1i5jeycPhhzGmVzyIrK1bhfB-5ErvTKRUi-Q4HBfCt2h1iz6-uWlFYdBoy4BPCJw";
        Map<String, Object> parse = parse(token);
        System.out.println(parse);
    }
}
